package cn.sumpu.app.guoguojie.util;

import java.text.SimpleDateFormat;
import java.util.*;

public class Experience {
	//总经验条占用的比例
	public static final Double ONE = 0.042586;
	public static final Double TWO = 0.056782;
	public static final Double THREE = 0.072555;
	public static final Double FOUR = 0.080441;
	public static final Double FIVE = 0.104100;
	public static final Double SIX = 0.107255;
	public static final Double SEVEN = 0.129337;
	public static final Double EIGHT = 0.154574;
	public static final Double NINE = 0.181388;
	public static final Double TEN = 0.067823;
	
	// 经验的级别分别对应的分数(1-10)
	public static final Integer LEVEL1 = 20; // LV1健康家庭
	public static final Integer LEVEL2 = 400; // LV2绿色家庭
	public static final Integer LEVEL3 = 1500; // LV3平安家庭
	public static final Integer LEVEL4 = 4000; // LV4优秀家庭
	public static final Integer LEVEL5 = 9000; // LV5文明家庭
	public static final Integer LEVEL6 = 18000; // LV6和睦家庭
	public static final Integer LEVEL7 = 28500; // LV7幸福家庭
	public static final Integer LEVEL8 = 43000; // LV8五好家庭
	public static final Integer LEVEL9 = 65000; // LV9十佳家庭
	public static final Integer LEVEL10 = 95000; // LV10和谐家庭
	
	// 定义加分的规则(第一段是一次性的加分；第三段是有上限的加分)
	public static final Integer createFamily = 20; // 创建家庭
	public static final Integer addMember = 20; // 添加家庭成员
	public static final Integer updateFamilyAvatar = 20; // 上传全家福
	public static final Integer updateFatherAvatar = 10; // 更新爸爸头像
	public static final Integer updateMotherAvatar = 10; // 更新妈妈头像
	public static final Integer addFamilyTag = 20; // 添加家庭标签（父母或宝宝）
	public static final Integer joinCrowd = 10; // 加入至少一个群组
	public static final Integer fatherEmailValidate = 10; // 爸爸的邮件验证
	public static final Integer motherEmailValidate = 10; // 妈妈的邮件验证

	public static final Integer loginDays = 5; // 连续3天登录果果街
	public static final Integer doingDays = 5; // 连续3天发表广播
	public static final Integer blogDays = 5; // 连续3天发表日志
	public static final Integer doingCommentTen = 5; // 一天内广播被10~50人评论（10≤评论数＜50）
	public static final Integer doingCommentFifty = 5; // 一天内广播被50人以上评论（>=50评论数）
	public static final Integer blogCommentTen = 5; // 一天内日志被10~50人评论（10≤评论数＜50）
	public static final Integer blogCommentFifty = 5; // 一天内日志被10~50人评论（10≤评论数＜50）

	public static final Integer loginGuoguojie = 1; // 登录网站
	public static final Integer loginGuoguojieCeiling = 2; // 登录网站 每日上限
	public static final Integer addFriend = 2; // 添加邻居（成功）
	public static final Integer addFriendCeiling = 8; // 添加邻居（成功）每日上限
	public static final Integer postDoing = 1; // 发表广播（包括评论）
	public static final Integer postDoingCeiling = 10; // 发表广播（包括评论）每日上限
	public static final Integer postBlog = 1; // 发表日志（包括评论）
	public static final Integer postBlogCeiling = 5; // 发表日志（包括评论）每日上限
	public static final Integer inviteFriend = 2; // 邀请好友成功加入
	public static final Integer inviteFriendCeiling = 8; // 邀请好友成功加入 每日上限
	public static final Integer invitedFriend = 2; // 被要求并成功和其他家庭成为邻居
	public static final Integer invitedFriendCeiling = 8; // 被要求并成功和其他家庭成为邻居
															// 每日上限
	// 通过经验得到级别
	public static Integer getLevelByIntegral(Integer integral) {
		try {
			if(integral==null || integral<0){
				return 0;
			}
			Integer level = 0;
			if (integral >= LEVEL1) {
				level = 1;
				if (integral >= LEVEL2) {
					level = 2;
					if (integral >= LEVEL3) {
						level = 3;
						if (integral >= LEVEL4) {
							level = 4;
							if (integral >= LEVEL5) {
								level = 5;
								if (integral >= LEVEL6) {
									level = 6;
									if (integral >= LEVEL7) {
										level = 7;
										if (integral >= LEVEL8) {
											level = 8;
											if (integral >= LEVEL9) {
												level = 9;
												if (integral >= LEVEL10) {
													level = 10;
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			return level;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	//计算还有多少分升级
	public static Integer getPointByIntegral(Integer integral) {
		try {
			if(integral==null || integral<0){
				return 1;
			}
			Integer point = LEVEL1-integral;
			if (integral >= LEVEL1) {
				point = LEVEL2-integral;
				if (integral >= LEVEL2) {
					point = LEVEL3-integral;
					if (integral >= LEVEL3) {
						point = LEVEL4-integral;
						if (integral >= LEVEL4) {
							point = LEVEL5-integral;
							if (integral >= LEVEL5) {
								point = LEVEL6-integral;
								if (integral >= LEVEL6) {
									point = LEVEL7-integral;
									if (integral >= LEVEL7) {
										point = LEVEL8-integral;
										if (integral >= LEVEL8) {
											point = LEVEL9-integral;
											if (integral >= LEVEL9) {
												point = LEVEL10-integral;
												if (integral >= LEVEL10) {
													point = 0;
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			return point;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	//计算所处级别的百分比
	public static Integer getPercent(Integer integral){
		try {
			if(integral==null || integral<0){
				return 1;
			}
			double level = (double)(LEVEL1-integral)/LEVEL1;
			if (integral >= LEVEL1) {
				level = (double)(integral-LEVEL1)/(LEVEL2-LEVEL1);
				if (integral >= LEVEL2) {
					level = (double)(integral-LEVEL2)/(LEVEL3-LEVEL2);
					if (integral >= LEVEL3) {
						level = (double)(integral-LEVEL3)/(LEVEL4-LEVEL3);
						if (integral >= LEVEL4) {
							level = (double)(integral-LEVEL4)/(LEVEL5-LEVEL4);
							if (integral >= LEVEL5) {
								level = (double)(integral-LEVEL5)/(LEVEL6-LEVEL5);
								if (integral >= LEVEL6) {
									level = (double)(integral-LEVEL6)/(LEVEL7-LEVEL6);
									if (integral >= LEVEL7) {
										level = (double)(integral-LEVEL7)/(LEVEL8-LEVEL7);
										if (integral >= LEVEL8) {
											level = (double)(integral-LEVEL8)/(LEVEL9-LEVEL8);
											if (integral >= LEVEL9) {
												level = (double)(integral-LEVEL9)/(LEVEL10-LEVEL9);
												if (integral >= LEVEL10) {
													level = 1;
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			return (int)Math.round(level*100);
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	private static Double getSum(Integer flag){
		double sum = 0.0;
		for(int i=1; i<=flag; i++){
			if(i==1){
				sum += ONE;
			}else if(i==2){
				sum += TWO;
			}else if(i==2){
				sum += TWO;
			}else if(i==3){
				sum += THREE;
			}else if(i==4){
				sum += FOUR;
			}else if(i==5){
				sum += FIVE;
			}else if(i==6){
				sum += SIX;
			}else if(i==7){
				sum += SEVEN;
			}else if(i==8){
				sum += EIGHT;
			}else if(i==9){
				sum += NINE;
			}else if(i==10){
				sum += TEN;
			}
		}
		return sum;
	}
	
	//计算占用所有经验条的比例
	public static Double getPercentAll(Integer integral){
		Double percentAll = 0.0000;	//结果
		//取得等级
		int level = getLevelByIntegral(integral);
		int percent = 0;
		if(level == 0){
			percent = getPercent(integral);
			percentAll = ONE*((double)percent/100);
		}else if(level == 1){
			percent = getPercent(integral);
			percentAll = getSum(1)+TWO*((double)percent/100);
		}else if(level == 2){
			percent = getPercent(integral);
			percentAll = getSum(2)+THREE*((double)percent/100);
		}else if(level == 3){
			percent = getPercent(integral);
			percentAll = getSum(3)+FOUR*((double)percent/100);
		}else if(level == 4){
			percent = getPercent(integral);
			percentAll = getSum(4)+FIVE*((double)percent/100);
		}else if(level == 5){
			percent = getPercent(integral);
			percentAll = getSum(5)+SIX*((double)percent/100);
		}else if(level == 6){
			percent = getPercent(integral);
			percentAll = getSum(6)+SEVEN*((double)percent/100);
		}else if(level == 7){
			percent = getPercent(integral);
			percentAll = getSum(7)+EIGHT*((double)percent/100);
		}else if(level == 8){
			percent = getPercent(integral);
			percentAll = getSum(8)+NINE*((double)percent/100);
		}else if(level == 9){
			percent = getPercent(integral);
			percentAll = getSum(9)+TEN*((double)percent/100);
		}else if(level == 10){
			percentAll = 1.0;
		}
		return percentAll*100;
	}
	
	// 通过经验得到级别的logo(暂时没用)
	public static String getLevelLogoByIntegral(Integer integral) {
		try {
			String levelLogo = "images/badge/one.jpg";
			if(integral==null || integral<0){
				return levelLogo;
			}
			if (integral >= LEVEL1) {
				levelLogo = "images/badge/one.jpg";
				if (integral >= LEVEL2) {
					levelLogo = "images/badge/two.jpg";
					if (integral >= LEVEL3) {
						levelLogo = "images/badge/three.jpg";
						if (integral >= LEVEL4) {
							levelLogo = "images/badge/four.jpg";
							if (integral >= LEVEL5) {
								levelLogo = "images/badge/five.jpg";
								if (integral >= LEVEL6) {
									levelLogo = "images/badge/six.jpg";
									if (integral >= LEVEL7) {
										levelLogo = "images/badge/seven.jpg";
										if (integral >= LEVEL8) {
											levelLogo = "images/badge/eight.jpg";
											if (integral >= LEVEL9) {
												levelLogo = "images/badge/nine.jpg";
												if (integral >= LEVEL10) {
													levelLogo = "images/badge/ten.jpg";
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			return levelLogo;
		} catch (Exception e) {
			e.printStackTrace();
			return "images/badge/one.jpg";
		}
	}

	/**
	 * author Bruce Zhang
	 * @param rookieReward 新手奖励的标记
	 * @param target 要验证的新手的某个操作
	 * @return 查看是否是第一次完成此操作(如果是第一次返回true，否则返回false)
	 */
	public static Boolean isOnce(String rookieReward, Integer target) {
		try { 
			if(Common.emptyStr(rookieReward) || target==null || target<=0){
				return false;
			} 
			if(rookieReward.charAt(--target)=='0'){
				return true;
			}else{
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * author Bruce Zhang
	 * @param rookieReward 原标记
	 * @param target 要改变的标记的索引
	 * @return 返回改变后的标记
	 */
	public static String replaceRookieReward(String rookieReward, Integer target) {
		try {
			if(Common.emptyStr(rookieReward) || target==null || target<=0){
				return rookieReward;
			}
			target--;
			rookieReward = rookieReward.substring(0,target)+"1"+rookieReward.substring(++target);
			return rookieReward;
		} catch (Exception e) {
			e.printStackTrace();
			return rookieReward;
		}
	}
	
	/**
	 * author Bruce Zhang
	 * @param date1	date1必须得早于date2
	 * @param date2	
	 * @return	判断两个日期相差几天
	 */
	public static Long getChaByTwoTime(Date date1, Date date2){
		try{
			if(date1==null || date2==null){
				return 2l;
			}
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			date1 = sdf.parse(sdf.format(date1));
			date2 = sdf.parse(sdf.format(date2));
			long cha = date2.getTime() - date1.getTime();
			long day = cha/(1000*60*60*24);
			return day;
		}catch(Exception e){
			e.printStackTrace();
			return 2l;
		}
	}
	
	public static void main(String[] args) {
		System.out.println(replaceRookieReward("00000", 5));
	}

}
